package com.heima.listener;

import cn.hutool.core.util.ObjectUtil;
import com.heima.model.constants.RabbitMQConstants;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.service.NewsService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 队列监听器 - 实现延时发布
 *
 * @author chenshan
 */
@Component
public class RabbitMQListener {
    @Autowired
    private NewsService newsService;

    /**
     * 文章的延时发布
     * @param newsId 文章id
     */
    @RabbitListener(queues = RabbitMQConstants.RABBITMQ_DELAY_QUEUE)
    public void publishNews(Integer newsId) {
        WmNews news = newsService.getById(newsId);
        if (ObjectUtil.isNotEmpty(news)) {
            newsService.publishNewsToArticle(news);
        }
    }

    /**
     * 文章的立即审核
     * @param newsId 文章id
     * @throws Exception 异常
     */
    @RabbitListener(queues = RabbitMQConstants.RABBITMQ_DELAY_RELEASE_QUEUE)
    public void newsAutoRelease(Integer newsId) throws Exception {
        WmNews news = newsService.getById(newsId);
        newsService.newsContentDetect(news);
    }
}
